package com.example.administrator.ygddriver.into.login.driverjoin.small.smallmessage.credentials;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.UserBean;
import com.example.administrator.ygddriver.liteorm.LiteOrmTools;
import com.example.administrator.ygddriver.map.ReachDestinationRequestBean;
import com.example.administrator.ygddriver.tool.CustomPopWindow;
import com.example.administrator.ygddriver.tool.GlideImageLoader;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.SimpleToolbar;
import com.example.administrator.ygddriver.tool.UrlValuse;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 上传照片界面
 */
public class CredentialsActivity extends BaseActivity implements View.OnClickListener {
    private SimpleToolbar simpleToolbar;
    private RelativeLayout mFrontFaceRl, mReverseSideRl, mOneCarPhotoRl, mHandheldRl, mVehiclelicenseRl, mDriverLicenseRl;
    private ImageView mFrontFaceIv, mReverseSideIv, mOneCarPhotoIv, mHandheldIv, mDriverLicenseIv, mVehiclelicenseIv;
    private static int ONE_CAR_PHOTO = 12312, FRONT_FACE = 12311, REVERSE_SIDE = 12313, HANDHELD = 12314, VEHICLE_LICENSE = 12315, DRIVER_LICENSE = 12316;
    private String mOneCarPhotoBase64 = null, mFrontFaceBase64 = null, mReverseSideBase64 = null, mHandheldBase64 = null, mDriverLicenseBase64 = null, mVehiclelicenseBase64 = null;
    private ImagePicker imagePicker;
    private Uri imageUri;
    private String mToken, mAccount;
    private TextView mNextTv;
    private int isSuccess = 0, noSuccess = 0;
    private PhotoBean mPhotoBean;
    private Bitmap ONECARPHOTO_bm, FRONTFACE_bm, REVERSESIDE_bm, HANDHELD_bm, VEHICLE_LICENSE_bm, DRIVER_LICENSE_bm;
    //判断是从重量还是小件过来的 1为小件，2为重量
    private String pdtype;
    private LinearLayout linearLayout;
    private CustomPopWindow mCustomPopWindow;

    @Override
    public int initLayout() {
        return R.layout.activity_credentials;
    }

    @Override
    public void initView() {
        linearLayout = findViewById(R.id.activity_credentials_driverlicense_ll);
        simpleToolbar = findViewById(R.id.activity_credentials_stb);
        mFrontFaceIv = findViewById(R.id.activity_credentials_front_face_iv);
        mFrontFaceRl = findViewById(R.id.activity_credentials_front_face_rl);
        mReverseSideRl = findViewById(R.id.activity_credentials_reverse_side_rl);
        mReverseSideIv = findViewById(R.id.activity_credentials_reverse_side_iv);
        mOneCarPhotoRl = findViewById(R.id.activity_credentials_one_car_photo_rl);
        mOneCarPhotoIv = findViewById(R.id.activity_credentials_one_car_photo_iv);
        mHandheldIv = findViewById(R.id.activity_credentials_handheld_iv);
        mHandheldRl = findViewById(R.id.activity_credentials_handheld_rl);
        mNextTv = findViewById(R.id.activity_credentials_next_tv);
        mDriverLicenseIv = findViewById(R.id.activity_credentials_driverlicense_iv);
        mDriverLicenseRl = findViewById(R.id.activity_credentials_driverlicense_rl);
        mVehiclelicenseIv = findViewById(R.id.activity_credentials_vehiclelicense_iv);
        mVehiclelicenseRl = findViewById(R.id.activity_credentials_vehiclelicense_rl);
        mDriverLicenseRl.setOnClickListener(this);
        mVehiclelicenseRl.setOnClickListener(this);
        mFrontFaceRl.setOnClickListener(this);
        mReverseSideRl.setOnClickListener(this);
        mOneCarPhotoRl.setOnClickListener(this);
        mHandheldRl.setOnClickListener(this);
        mNextTv.setOnClickListener(this);
        mPhotoBean = new PhotoBean();

    }

    @Override
    public void initDate() {
        pdtype = getIntent().getStringExtra("tag");
        if (pdtype.equals("1")) {
            linearLayout.setVisibility(View.GONE);
        } else {
            linearLayout.setVisibility(View.VISIBLE);
        }
        imagePicker = ImagePicker.getInstance();
        setSimpleToolbar();
        setIgPicker();
        LiteOrmTools.getInformation(UserBean.class, this, new LiteOrmTools.On<UserBean>() {
            @Override
            public void hasData(UserBean data) {
                mToken = data.getC_token();
                mAccount = data.getC_account();
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });
        String s = String.valueOf(Environment.getExternalStorageDirectory());
        imagePicker.getImageLoader().displayImage(this, s + "/YGDDriver/Photo/" + mAccount + "ONE_CAR_PHOTO.jpg", mOneCarPhotoIv, mOneCarPhotoIv.getWidth(), mOneCarPhotoIv.getHeight());
        imagePicker.getImageLoader().displayImage(this, s + "/YGDDriver/Photo/" + mAccount + "FRONT_FACE.jpg", mFrontFaceIv, mFrontFaceIv.getWidth(), mFrontFaceIv.getHeight());
        imagePicker.getImageLoader().displayImage(this, s + "/YGDDriver/Photo/" + mAccount + "REVERSE_SIDE.jpg", mReverseSideIv, mReverseSideIv.getWidth(), mReverseSideIv.getHeight());
        imagePicker.getImageLoader().displayImage(this, s + "/YGDDriver/Photo/" + mAccount + "HANDHELD.jpg", mHandheldIv, mHandheldIv.getWidth(), mHandheldIv.getHeight());
        imagePicker.getImageLoader().displayImage(this, s + "/YGDDriver/Photo/" + mAccount + "VEHICLE_LICENSE.jpg", mVehiclelicenseIv, mVehiclelicenseIv.getWidth(), mVehiclelicenseIv.getHeight());
        imagePicker.getImageLoader().displayImage(this, s + "/YGDDriver/Photo/" + mAccount + "DRIVER_LICENSE.jpg", mDriverLicenseIv, mDriverLicenseIv.getWidth(), mDriverLicenseIv.getHeight());
    }

    private void setSimpleToolbar() {
        simpleToolbar.setMainTitle("上传照片");
        simpleToolbar.setMainTitleColor("#000000");
        simpleToolbar.setBackIv(R.mipmap.left_title);
        simpleToolbar.setToolColor("#ffffff");
        simpleToolbar.setBackIvClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.activity_credentials_front_face_rl:
                Intent intent = new Intent(this, ImageGridActivity.class);
                startActivityForResult(intent, FRONT_FACE);
                break;
            case R.id.activity_credentials_reverse_side_rl:
                Intent intent2 = new Intent(this, ImageGridActivity.class);
                startActivityForResult(intent2, REVERSE_SIDE);
                break;

            case R.id.activity_credentials_one_car_photo_rl:
                Intent intent3 = new Intent(this, ImageGridActivity.class);
                startActivityForResult(intent3, ONE_CAR_PHOTO);
                break;
            case R.id.activity_credentials_handheld_rl:
                Intent intent4 = new Intent(this, ImageGridActivity.class);
                startActivityForResult(intent4, HANDHELD);
                break;
            case R.id.activity_credentials_driverlicense_rl:
                Intent intent5 = new Intent(this, ImageGridActivity.class);
                startActivityForResult(intent5, DRIVER_LICENSE);
                break;
            case R.id.activity_credentials_vehiclelicense_rl:
                Intent intent6 = new Intent(this, ImageGridActivity.class);
                startActivityForResult(intent6, VEHICLE_LICENSE);
                break;
            case R.id.activity_credentials_next_tv:
                setPopW(getWindow().getDecorView());
                isSuccess = 0;
                noSuccess = 0;
                String s = String.valueOf(Environment.getExternalStorageDirectory());
                if (TextUtils.isEmpty(mOneCarPhotoBase64)) {
                    if (BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "ONE_CAR_PHOTO.jpg") != null) {
                        mOneCarPhotoBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "ONE_CAR_PHOTO.jpg")));
                        ONECARPHOTO_bm = comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "ONE_CAR_PHOTO.jpg"));
                        initFileHtml(mOneCarPhotoBase64, "6", mToken, 1);
                        isSuccess++;
                    }
                } else {
                    initFileHtml(mOneCarPhotoBase64, "6", mToken, 1);
                    isSuccess++;
                }
                if (TextUtils.isEmpty(mHandheldBase64)) {
                    if (BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "HANDHELD.jpg") != null) {
                        mHandheldBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "HANDHELD.jpg")));
                        HANDHELD_bm = comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "HANDHELD.jpg"));
                        initFileHtml(mHandheldBase64, "5", mToken, 2);
                        isSuccess++;
                    }
                } else {
                    initFileHtml(mHandheldBase64, "5", mToken, 2);
                    isSuccess++;
                }
                if (TextUtils.isEmpty(mReverseSideBase64)) {
                    if (BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "REVERSE_SIDE.jpg") != null) {
                        mReverseSideBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "REVERSE_SIDE.jpg")));
                        REVERSESIDE_bm = comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "REVERSE_SIDE.jpg"));
                        initFileHtml(mReverseSideBase64, "4", mToken, 3);
                        isSuccess++;
                    }

                } else {
                    initFileHtml(mReverseSideBase64, "4", mToken, 3);
                    isSuccess++;
                }
                if (TextUtils.isEmpty(mFrontFaceBase64)) {
                    if (BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "FRONT_FACE.jpg") != null) {
                        mFrontFaceBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "FRONT_FACE.jpg")));
                        FRONTFACE_bm = comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "FRONT_FACE.jpg"));
                        initFileHtml(mFrontFaceBase64, "4", mToken, 4);
                        isSuccess++;
                    }

                } else {
                    initFileHtml(mFrontFaceBase64, "4", mToken, 4);
                    isSuccess++;
                }
                if (pdtype.equals("2")) {
                    if (TextUtils.isEmpty(mDriverLicenseBase64)) {
                        if (BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "DRIVER_LICENSE.jpg") != null) {
                            mDriverLicenseBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "DRIVER_LICENSE.jpg")));
                            DRIVER_LICENSE_bm = comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "DRIVER_LICENSE.jpg"));
                            initFileHtml(mDriverLicenseBase64, "7", mToken, 5);
                            isSuccess++;
                        }
                    } else {
                        initFileHtml(mDriverLicenseBase64, "7", mToken, 5);
                        isSuccess++;
                    }
                    if (TextUtils.isEmpty(mVehiclelicenseBase64)) {
                        if (BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "VEHICLE_LICENSE.jpg") != null) {
                            mVehiclelicenseBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "VEHICLE_LICENSE.jpg")));
                            VEHICLE_LICENSE_bm = comp(BitmapFactory.decodeFile(s + "/YGDDriver/Photo/" + mAccount + "VEHICLE_LICENSE.jpg"));
                            initFileHtml(mVehiclelicenseBase64, "8", mToken, 6);
                            isSuccess++;
                        }
                    } else {
                        isSuccess++;
                        initFileHtml(mVehiclelicenseBase64, "8", mToken, 6);
                    }
                }
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == ONE_CAR_PHOTO) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                mOneCarPhotoBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(images.get(0).path)));
                imagePicker.getImageLoader().displayImage(this, images.get(0).path, mOneCarPhotoIv, mOneCarPhotoIv.getWidth(), mOneCarPhotoIv.getHeight());
                ONECARPHOTO_bm = comp(BitmapFactory.decodeFile(images.get(0).path));
//                savePhotoToSD(ONECARPHOTO_bm, mAccount + "ONE_CAR_PHOTO");
            } else {
//                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
            if (data != null && requestCode == HANDHELD) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                mHandheldBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(images.get(0).path)));
                imagePicker.getImageLoader().displayImage(this, images.get(0).path, mHandheldIv, mHandheldIv.getWidth(), mHandheldIv.getHeight());
                HANDHELD_bm = comp(BitmapFactory.decodeFile(images.get(0).path));
//                savePhotoToSD(HANDHELD_bm, mAccount + "HANDHELD");
            } else {
//                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
            if (data != null && requestCode == REVERSE_SIDE) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                mReverseSideBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(images.get(0).path)));
                imagePicker.getImageLoader().displayImage(this, images.get(0).path, mReverseSideIv, mReverseSideIv.getWidth(), mReverseSideIv.getHeight());
                REVERSESIDE_bm = comp(BitmapFactory.decodeFile(images.get(0).path));
//                savePhotoToSD(REVERSESIDE_bm, mAccount + "REVERSE_SIDE");

            } else {
//                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
            if (data != null && requestCode == FRONT_FACE) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                mFrontFaceBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(images.get(0).path)));
                imagePicker.getImageLoader().displayImage(this, images.get(0).path, mFrontFaceIv, mFrontFaceIv.getWidth(), mFrontFaceIv.getHeight());
                FRONTFACE_bm = comp(BitmapFactory.decodeFile(images.get(0).path));
//                savePhotoToSD(FRONTFACE_bm, mAccount + "FRONT_FACE");

            } else {
//                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
            if (data != null && requestCode == VEHICLE_LICENSE) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                mVehiclelicenseBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(images.get(0).path)));
                imagePicker.getImageLoader().displayImage(this, images.get(0).path, mVehiclelicenseIv, mVehiclelicenseIv.getWidth(), mVehiclelicenseIv.getHeight());
                VEHICLE_LICENSE_bm = comp(BitmapFactory.decodeFile(images.get(0).path));
//                savePhotoToSD(FRONTFACE_bm, mAccount + "FRONT_FACE");

            } else {
//                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
            if (data != null && requestCode == DRIVER_LICENSE) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                mDriverLicenseBase64 = bitmapToBase64(comp(BitmapFactory.decodeFile(images.get(0).path)));
                imagePicker.getImageLoader().displayImage(this, images.get(0).path, mDriverLicenseIv, mDriverLicenseIv.getWidth(), mDriverLicenseIv.getHeight());
                DRIVER_LICENSE_bm = comp(BitmapFactory.decodeFile(images.get(0).path));
//                savePhotoToSD(FRONTFACE_bm, mAccount + "FRONT_FACE");

            } else {
//                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }

    //上传图片
    private void initFileHtml(String fileBase64Str, final String type, String c_token, final int tag) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("fileBase64Str", fileBase64Str);
        hashMap.put("type", type);
        hashMap.put("c_token", c_token);
        hashMap.put("c_type", "2");
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_UPLOAD + UrlValuse.FILE, hashMap, FileRrequestBean.class, new OkGoRequests.OnTrue<FileRrequestBean>() {
            @Override
            public void hasData(FileRrequestBean date) {
                if (date.isSuccess()) {
                    if (tag == 1) {
                        savePhotoToSD(ONECARPHOTO_bm, mAccount + "ONE_CAR_PHOTO");
                        mPhotoBean.setC_onecarphoto(date.getResult().getDbPath());
                    } else if (tag == 2) {
                        savePhotoToSD(HANDHELD_bm, mAccount + "HANDHELD");
                        mPhotoBean.setC_handheld(date.getResult().getDbPath());
                    } else if (tag == 3) {
                        savePhotoToSD(REVERSESIDE_bm, mAccount + "REVERSE_SIDE");
                        mPhotoBean.setC_reverseside(date.getResult().getDbPath());
                    } else if (tag == 4) {
                        savePhotoToSD(FRONTFACE_bm, mAccount + "FRONT_FACE");
                        mPhotoBean.setC_frontface(date.getResult().getDbPath());
                    } else if (tag == 6) {
                        savePhotoToSD(VEHICLE_LICENSE_bm, mAccount + "VEHICLE_LICENSE");
                        mPhotoBean.setC_vehiclelicense(date.getResult().getDbPath());
                    } else if (tag == 5) {
                        savePhotoToSD(DRIVER_LICENSE_bm, mAccount + "DRIVER_LICENSE");
                        mPhotoBean.setC_driverlicense(date.getResult().getDbPath());
                    }
                    isSuccess--;
                } else {
                    noSuccess--;
                }
                if (pdtype.equals("1")) {
                    if (isSuccess == 0) {
                        mCustomPopWindow.dissmiss();
                        Toast.makeText(CredentialsActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                        //创建Intent对象
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("result", mPhotoBean);
                        intent.putExtras(bundle);
                        //返回结果
                        setResult(0x001, intent);
                        finish();
                    }
                } else {
                    if (isSuccess == 0) {
                        mCustomPopWindow.dissmiss();
                        Toast.makeText(CredentialsActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                        //创建Intent对象
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("result", mPhotoBean);
                        intent.putExtras(bundle);
                        //返回结果
                        setResult(0x001, intent);
                        finish();
                    }
                }

            }
        }, new OkGoRequests.OnError<FileRrequestBean>() {
            @Override
            public void noData() {

            }
        });
    }

    public static String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    private Bitmap comp(Bitmap image) {
        float scale = 768f / (float) image.getWidth();
        return compressImage(Bitmap.createScaledBitmap(image, 768,
                (int) (image.getHeight() * scale), false));
    }

    private Bitmap compressImage(Bitmap image) {

        Bitmap bitmap = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 40) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset();// 重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%，把压缩后的数据存放到baos中
            options -= 10;// 每次都减少10
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(
                baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
        bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片

        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            isBm.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    /**
     * 保存照片到SD卡的指定位置
     */
    private void savePhotoToSD(Bitmap bitmap, String name) {
        File outputImage = new File(Environment.getExternalStorageDirectory(), "/YGDDriver/Photo/" + name + ".jpg");
        if (!outputImage.getParentFile().exists()) {
            outputImage.getParentFile().mkdirs();
        }
        try {
            FileOutputStream fos = new FileOutputStream(outputImage);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void setIgPicker() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);                      //显示拍照按钮
        imagePicker.setCrop(true);                           //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true);                   //是否按矩形区域保存
        imagePicker.setSelectLimit(1);              //选中数量限制
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(200);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(200);                         //保存文件的高度。单位像素
    }

    private void setPopW(View v) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_activity_send_map_popw_uping, null);
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this).setView(view)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                .enableOutsideTouchableDissmiss(false)// 设置点击PopupWindow之外的地方，popWindow不关闭，如果不设置这个属性或者为true，则关闭
                .create()
                .showAtLocation(v, Gravity.CENTER, 0, 0);
    }

}
